package de.koller.oceanFramework.plain.util;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class RFrame extends Canvas {
	private static final long serialVersionUID = 1L;
	
	private final IRRender node;
	
	private JFrame container = null;
	private BufferStrategy strategy;
	
	public final int width, height;
	private final Container panel;
	
	public RFrame( int width, int height, IRRender node ) {
		this.node = node;

		this.container = new JFrame();
		this.panel = container.getContentPane();
		
		this.panel.setPreferredSize( new Dimension( width, height ) );
		this.panel.setLayout( null );
		
		this.setBounds( 0, 0, width, height );
		this.panel.add( this );
		this.setIgnoreRepaint( true );
		
		
		this.container.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
		this.container.pack();
		this.container.setResizable( false );
			
		this.container.addWindowListener( new WindowAdapter() {
			@Override public void windowClosing( WindowEvent e ) {
				RFrame.this.node.close();
			}
		} );
		
		this.width = width;
		this.height = height;
	}

	public void init() {
		container.setVisible( true );
		container.pack();

		container.setLocation( (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2d - container.getWidth()/2d ),
				(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2d - container.getHeight()/2d ) );
		
		this.createBufferStrategy( 3 );
		strategy = this.getBufferStrategy();
		
		this.requestFocus();
	}
	
	public void release() {
		container.dispose();
	}
	
	public void render( int delta ) {
		Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
		//g.setColor( Color.BLACK );
		//g.fillRect( 0, 0, width, height );

		node.renderContent( g );

		g.dispose();
		strategy.show();
	}
	
	public void setTitle( String title ) {
		container.setTitle( title );
	}

}